import { spawn } from 'node:child_process'

import { validateArtifacts } from '../artifacts'
import { createSystem } from '../lib/system'
import { ExitError, importBuiltKeystoneConfiguration } from './utils'

async function spawnPrisma3(
  cwd: string,
  system: {
    config: {
      db: {
        url: string
      }
    }
  },
  commands: string[]
) {
  return new Promise<{
    exitCode: number | null
  }>((resolve, reject) => {
    const p = spawn(
      'node',
      ['--title=prisma', require.resolve('prisma/build/index.js'), ...commands],
      {
        cwd,
        env: {
          ...process.env,
          DATABASE_URL: system.config.db.url,
          PRISMA_HIDE_UPDATE_MESSAGE: '1',
        },
        stdio: 'inherit',
      }
    )
    p.on('error', err => reject(err))
    p.on('exit', exitCode => resolve({ exitCode }))
  })
}

export async function prisma(cwd: string, args: string[], frozen: boolean) {
  // TODO: should build unless --frozen?

  const system = createSystem(await importBuiltKeystoneConfiguration(cwd))
  await validateArtifacts(cwd, system)

  const { exitCode } = await spawnPrisma3(cwd, system, args)
  if (typeof exitCode === 'number' && exitCode !== 0) {
    throw new ExitError(exitCode)
  }
}
